package com.itheima.reggie.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public R add(DishDto dishDto) {
        //校验参数
        if (dishDto == null) {
            return R.error("参数错误");
        }
        //将dishDto中的数据复制给dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);

        dishMapper.insert(dish);
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
            dishFlavorMapper.insert(flavor);
        }
        return R.success("添加成功");
    }

    @Override
    public R findPage(Integer page, Integer pageSize, String name) {
        if (null == name) {
            Page<Dish> p = new Page<>(page, pageSize);
            Page<Dish> dishPage = dishMapper.selectPage(p, null);
            List<Dish> dishList = dishPage.getRecords();
            String jsonString = JSON.toJSONString(dishList);
            List<DishDto> dishDtos = JSON.parseArray(jsonString, DishDto.class);
            for (DishDto dishDto : dishDtos) {
                Category category = categoryMapper.selectById(dishDto.getCategoryId());
                dishDto.setCategoryName(category.getName());
            }
            //封装返回值对象
            String jsonString1 = JSON.toJSONString(dishPage);
            Page pageInfo = JSON.parseObject(jsonString1, Page.class);
            pageInfo.setRecords(dishDtos);
            return R.success(pageInfo);
        }
        return null;
    }

    @Override
    public R findById(Long id) {

        Dish dish = dishMapper.selectById(id);

        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);

        Category category = categoryMapper.selectById(dish.getCategoryId());

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        dishDto.setCategoryName(category.getName());
        dishDto.setFlavors(dishFlavors);
        return R.success(dishDto);
    }

    @Override
    public void modify(DishDto dishDto) {
        //菜品直接修改
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);
        dishMapper.updateById(dish);

        //口味先删后增
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(wrapper);

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
            dishFlavorMapper.insert(flavor);
        }
    }

    @Override
    public void remove(Long[] ids) {
        for (Long id : ids) {
            //先删除对应的口味
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.delete(wrapper);
            //再删除菜品
            dishMapper.deleteById(id);
        }
    }

    @Override
    public void modifyStatus(Long[] ids, Integer id) {
        Dish dish = new Dish();
        LambdaUpdateWrapper<Dish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Dish::getStatus, id).in(Dish::getId, ids);
        dishMapper.update(dish, wrapper);
    }

    @Override
    public R list(Long categoryId) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //todo 将dishList转为dishdto
        String jsonString = JSON.toJSONString(dishList);
        List<DishDto> dishDtos = JSON.parseArray(jsonString, DishDto.class);
        //todo 循环添加口味信息
        for (DishDto dishDto : dishDtos) {
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, dishDto.getId());
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
            dishDto.setFlavors(dishFlavors);
        }
        return R.success(dishDtos);
    }
}
